"""
@File: amap.py
@Project: 约个球
@Author: zhangjian
@Time: 2022.8.27 23:32
@Description: None
"""
import requests
from config.settings import GEO_URL, REGEO_URL, GEO_DISTANCE
from geopy.distance import geodesic


def geo_response(address=None):
    """根据地名返回经纬度"""
    if address is not None:
        url = f"{GEO_URL}&address={address}"
        response = requests.get(url)
        location = response.json()["geocodes"][0]["location"]
        location_x = float(location.split(',')[0])
        location_y = float(location.split(',')[1])
        return location_x, location_y


def geo_distance(x1, y1, x2, y2):
    """判断两地距离是否在30km以内"""
    distance = geodesic((y1, x1), (y2, x2)).km
    if distance <= GEO_DISTANCE:
        return True
    else:
        return False